﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ClipperBaseUI.Controls;
using Clipper.BLL;
using Global;
using System.Collections.ObjectModel;
using System.Text.RegularExpressions;
using Clipper.ClipperWindow;

namespace Clipper.ClipperWindow
{
    /// <summary>
    /// WinReagentBarcode.xaml 的交互逻辑
    /// </summary>
    public partial class WinReagentBarcode : BaseWindow
    {
        public WinReagentBarcode()
        {
            InitializeComponent();
        }

        DetectItems detectItems = new DetectItems();

        ReagentKit mReagentKitBll = new ReagentKit();
        Reagent mReagentBll = new Reagent();
        Model.ReagentKit mKitModel = null;
        BLL.Reagent DetectItems = null;
        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            if (!textBox.Text.Trim().Equals(""))
            {
                //存储ISI pt值
                Model.Reagent reagentMo = new Model.Reagent();
                reagentMo.reagentID = 1;
                reagentMo.PtISIValue = Convert.ToDouble(textBox.Text);
                DetectItems.UpdateISI(reagentMo);
                ServiceProvider.pTISIValue= Convert.ToDouble(textBox.Text); //给全局的变量赋值

            }
            else
            {
                //弹框提示输入ＩＳＩ值
                WinShowMessage.ShowDlg("警告", "请输入PT试剂的ISI的值", WinShowMessage.MsgBoxBtns.OK, WinShowMessage.MsgBoxIcon.Information);
                return;
            }

            int holeNum = Convert.ToInt32(HoleNum.Text);
            string regentName = reagentComboBoxTxt.Text.ToString();
            //拼接接受的数值
            //string connect =holeNum + regentName;

            //Regex r;
            Regex r2;
            //string pattern;
            //pattern = @"1|2|3|4|5|6|7|8|31|32";
            string detectNames = @"66";
            ObservableCollection<Clipper.Model.DetectItems> detectItem = detectItems.GetFullDetectItemsInfoList("");
            foreach (Clipper.Model.DetectItems det in detectItem)
            {
                string detectName = det.reagentName1;
                if (detectName != "")
                {
                    detectNames += "|" + detectName;
                }
            }
            //r = new Regex(pattern);
            r2 = new Regex(detectNames);


            if ((holeNum == 1 || holeNum == 2 || holeNum == 3 || holeNum == 4 || holeNum == 5 || holeNum == 6 || holeNum == 7 || holeNum == 8 || holeNum == 31 || holeNum == 32) && r2.IsMatch(regentName))
            {
                //System.Windows.MessageBox.Show("不能把 [" + regentName + "] 放到 [" + holeNum + "号] 孔位！！" + "\n" + "请放到9-30孔位或33-36孔位！！", "警告！！");
                WinNoticeBoard winbard = new WinNoticeBoard();
                winbard.Tag = "不能把 [" + regentName + "] 放到 [" + holeNum + "号] 孔位！！" + "\n" + "请放到9-30孔位或33-36孔位！！";

                winbard.ShowDialog();

            }
            else
            {
                //string regentName = reagentComboBoxTxt.Text.ToString();
                //string barcode2 = reagentComboBoxTxt.Text.ToString();
                Model.Reagent reagent1 = null;
                //Model.Reagent reagent2 = null;
                List<string> strErr = new List<string>();
                if (reagentComboBoxTxt.IsEnabled)
                {
                    if (regentName != "")
                    {
                        reagent1 = mReagentBll.GetModelByReagentName(regentName);
                        if (reagent1 != null)
                        {
                            //if (reagent1.StateId == 1)//判断是否已加载
                            //{
                            mKitModel.holeNumber = holeNum;   // 给孔位赋值 
                            mKitModel.ReagentID1 = reagent1.reagentID;
                            mKitModel.loadDate1 = DateTime.Now;
                            mKitModel.ReagentModel1 = reagent1;
                            mKitModel.loadDate2 = reagent1.ReagentLeft;
                            mKitModel.ReagentID2 = reagent1.brand;
                            mKitModel.ReagentName = reagent1.brand;
                            //}
                            //else
                            //{
                            //    strErr.Add("Reagent1LoadedTip");
                            //}
                        }
                        else
                        {
                            strErr.Add("Reagent1NotFound");
                        }
                    }
                    else
                    {
                        strErr.Add("Reagent1NotFound");
                    }
                }

                if (strErr.Count == 0)
                {
                    if (mReagentKitBll.Update(mKitModel))
                    {
                        this.Close();
                    }
                    else
                    {
                        this.Close();
                    }
                }
                else
                {
                    UIHelper.ShowKeyMessage(strErr);
                }
            }
        }
        private void BaseWindow_Loaded(object sender, RoutedEventArgs e)
        {
            textBox.Visibility = Visibility.Hidden; //隐藏文本框

            mKitModel = this.Tag as Model.ReagentKit;
            DetectItems = new Reagent();
            //if (mKitModel.ReagentModel1 != null)
            //{
            reagentComboBoxTxt.ItemsSource = DetectItems.GetModelList("");

            HoleNum.Text = mKitModel.holeNumber.ToString();
            //reagentComboBoxTxt.IsEnabled = false;
            //}
            //if (mKitModel.ReagentModel2 != null)
            //{
            //    txtKit2Barcode.Text = mKitModel.ReagentModel2.barcode;
            //    txtKit2Barcode.IsEnabled = false;
            //}
        }

        private void reagentComboBoxTxt_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (reagentComboBoxTxt.SelectedValue.Equals("PT凝血酶"))
            {
                textBox.Visibility = Visibility.Visible;
                textBlock.Text = "请输入ISI值：";
            }
        }
    }
}
